home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / sbin / dhclient-script < prev    next >
Encoding:
Text File  |  2012-09-23  |  7.8 KB  |  254 lines

  1. #!/bin/bash
  2.  
  3. # dhclient-script for Linux. Dan Halbert, March, 1997.
  4. # Updated for Linux 2.[12] by Brian J. Murrell, January 1999.
  5. # Modified for Debian.  Matt Zimmerman and Eloy Paris, December 2003
  6. # Modified to remove useless tests for antiquated kernel versions that
  7. # this doesn't even work with anyway, and introduces a dependency on /usr
  8. # being mounted, which causes cosmetic errors on hosts that NFS mount /usr
  9. # Andrew Pollock, February 2005
  10. # Modified to work on point-to-point links. Andrew Pollock, June 2005
  11. # Modified to support passing the parameters called with to the hooks. Andrew Pollock, November 2005
  12.  
  13. # The alias handling in here probably still sucks. -mdz
  14.  
  15. make_resolv_conf() {
  16.     if [ "$new_domain_name" -o "$new_domain_name_servers" ]; then
  17.         local new_resolv_conf=/etc/resolv.conf.dhclient-new
  18.         rm -f $new_resolv_conf
  19.         if [ "$new_domain_name" ]; then
  20.             echo domain ${new_domain_name%% *} >>$new_resolv_conf
  21.         fi
  22.     if [ "$new_domain_search" ]; then
  23.         #new_domain_search="${new_domain_search% }"
  24.         if [ "$new_domain_name" ]; then
  25.         domain_in_search_list=""
  26.         for domain in "$new_domain_search"; do
  27.             if [ "$domain" = "$new_domain_name" ]; then
  28.                 domain_in_search="Yes"
  29.             fi
  30.         done
  31.         if [ ! "$domain_in_search" ]; then
  32.                 new_domain_search="$new_domain_name $new_domain_search"
  33.         fi
  34.         fi
  35.         echo "search ${new_domain_search}" >> $new_resolv_conf
  36.     elif [ "$new_domain_name" ]; then
  37.         echo "search ${new_domain_name}" >> $new_resolv_conf
  38.     fi
  39.         if [ "$new_domain_name_servers" ]; then
  40.                    for nameserver in $new_domain_name_servers; do
  41.                        echo nameserver $nameserver >>$new_resolv_conf
  42.             done
  43.         else # keep 'old' nameservers
  44.             sed -n /^\w*[Nn][Aa][Mm][Ee][Ss][Ee][Rr][Vv][Ee][Rr]/p /etc/resolv.conf >>$new_resolv_conf
  45.         fi
  46.         chown --reference=/etc/resolv.conf $new_resolv_conf
  47.         chmod --reference=/etc/resolv.conf $new_resolv_conf
  48.         mv -f $new_resolv_conf /etc/resolv.conf
  49.     fi
  50. }
  51.  
  52. run_hook() {
  53.     local script="$1"
  54.     local exit_status
  55.     shift    # discard the first argument, then the rest are the script's
  56.  
  57.     if [ -f $script ]; then
  58.         . $script "$@"
  59.     fi
  60.  
  61.  
  62.     if [ -n "$exit_status" ] && [ "$exit_status" -ne 0 ]; then
  63.         logger -p daemon.err "$script returned non-zero exit status $exit_status"
  64.         save_exit_status=$exit_status
  65.     fi
  66.  
  67.     return $exit_status
  68. }
  69.  
  70. run_hookdir() {
  71.     local dir="$1"
  72.     local exit_status
  73.     shift    # See run_hook
  74.  
  75.     if [ -d "$dir" ]; then
  76.         for script in $(run-parts --list $dir); do
  77.             run_hook $script "$@" || true
  78.             exit_status=$?
  79.         done
  80.     fi
  81.  
  82.     return $exit_status
  83. }
  84.  
  85. # Must be used on exit.   Invokes the local dhcp client exit hooks, if any.
  86. exit_with_hooks() {
  87.     exit_status=$1
  88.  
  89.     # Source the documented exit-hook script, if it exists
  90.     if ! run_hook /etc/dhcp/dhclient-exit-hooks "$@"; then
  91.         exit_status=$?
  92.     fi
  93.  
  94.     # Now run scripts in the Debian-specific directory.
  95.     if ! run_hookdir /etc/dhcp/dhclient-exit-hooks.d "$@"; then
  96.         exit_status=$?
  97.     fi
  98.  
  99.     exit $exit_status
  100. }
  101.  
  102. if [ -n "$new_broadcast_address" ]; then
  103.     new_broadcast_arg="broadcast $new_broadcast_address"
  104. fi
  105. if [ -n "$old_broadcast_address" ]; then
  106.     old_broadcast_arg="broadcast $old_broadcast_address"
  107. fi
  108. if [ -n "$new_subnet_mask" ]; then
  109.     new_subnet_arg="netmask $new_subnet_mask"
  110. fi
  111. if [ -n "$old_subnet_mask" ]; then
  112.     old_subnet_arg="netmask $old_subnet_mask"
  113. fi
  114. if [ -n "$alias_subnet_mask" ]; then
  115.     alias_subnet_arg="netmask $alias_subnet_mask"
  116. fi
  117. # The 576 MTU is only used for X.25 and dialup connections
  118. # where the admin wants low latency.  Such a low MTU can cause
  119. # problems with UDP traffic, among other things.  As such,
  120. # disallow MTUs from 576 and below by default, so that broken
  121. # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
  122. if [ -n "$new_interface_mtu" ] && [ $new_interface_mtu -gt 576 ]; then
  123.     mtu_arg="mtu $new_interface_mtu"
  124. fi
  125. if [ -n "$IF_METRIC" ]; then
  126.     metric_arg="metric $IF_METRIC"    # interfaces(5), "metric" option
  127. fi
  128.  
  129.  
  130. # The action starts here
  131.  
  132. # Invoke the local dhcp client enter hooks, if they exist.
  133. run_hook /etc/dhcp/dhclient-enter-hooks
  134. run_hookdir /etc/dhcp/dhclient-enter-hooks.d
  135.  
  136. # Execute the operation
  137. case "$reason" in
  138.     MEDIUM|ARPCHECK|ARPSEND)
  139.         # Do nothing
  140.         ;;
  141.     PREINIT)
  142.         # The DHCP client is requesting that an interface be
  143.         # configured as required in order to send packets prior to
  144.         # receiving an actual address. - dhclient-script(8)
  145.  
  146.         if [ -n "$alias_ip_address" ]; then
  147.             # Bring down alias interface. Its routes will disappear too.
  148.             ifconfig $interface:0- inet 0
  149.         fi
  150.         ifconfig $interface inet 0 up
  151.  
  152.         ;;
  153.     BOUND|RENEW|REBIND|REBOOT)
  154.  
  155.     if [ -n "$old_host_name" -a -n "$host_name" -a \
  156.               "$host_name" != "$old_host_name" ]; then
  157.             hostname "$new_host_name"
  158.     fi
  159.  
  160.         if [ -n "$old_ip_address" -a -n "$alias_ip_address" -a \
  161.              "$alias_ip_address" != "$old_ip_address" ]; then
  162.             # Possible new alias. Remove old alias.
  163.             ifconfig $interface:0- inet 0
  164.         fi
  165.  
  166.         if [ -n "$old_ip_address" -a \
  167.              "$old_ip_address" != "$new_ip_address" ]; then
  168.             # IP address changed. Bringing down the interface will delete all routes,
  169.             # and clear the ARP cache.
  170.             ifconfig $interface inet 0
  171.  
  172.         fi
  173.  
  174.         if [ -z "$old_ip_address" -o "$old_ip_address" != "$new_ip_address" -o \
  175.             "$reason" = "BOUND" -o "$reason" = "REBOOT" ]; then
  176.  
  177.             ifconfig $interface inet $new_ip_address $new_subnet_arg \
  178.                 $new_broadcast_arg $mtu_arg
  179.  
  180.         # point to point
  181.         if [ "$new_subnet_mask" == "255.255.255.255" ]; then
  182.             for router in $new_routers; do
  183.                 route add -host $router dev $interface
  184.             done
  185.         fi
  186.  
  187.             for router in $new_routers; do
  188.                 route add default dev $interface gw $router $metric_arg
  189.             done
  190.         fi
  191.  
  192.         if [ "$new_ip_address" != "$alias_ip_address" -a -n "$alias_ip_address" ];
  193.             then
  194.             ifconfig $interface:0- inet 0
  195.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  196.             route add -host $alias_ip_address $interface:0
  197.         fi
  198.  
  199.         make_resolv_conf
  200.  
  201.         ;;
  202.  
  203.     EXPIRE|FAIL|RELEASE|STOP)
  204.         if [ -n "$alias_ip_address" ]; then
  205.             # Turn off alias interface.
  206.             ifconfig $interface:0- inet 0
  207.         fi
  208.  
  209.         if [ -n "$old_ip_address" ]; then
  210.             # Shut down interface, which will delete routes and clear arp cache.
  211.             ifconfig $interface inet 0
  212.         fi
  213.  
  214.         if [ -n "$alias_ip_address" ]; then
  215.             ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  216.             route add -host $alias_ip_address $interface:0
  217.         fi
  218.  
  219.         ;;
  220.  
  221.     TIMEOUT)
  222.         if [ -n "$alias_ip_address" ]; then
  223.             ifconfig $interface:0- inet 0
  224.         fi
  225.  
  226.         ifconfig $interface inet $new_ip_address $new_subnet_arg \
  227.             $new_broadcast_arg $mtu_arg
  228.  
  229.         set -- $new_routers
  230.         first_router="$1"
  231.  
  232.         if [ -z "$first_router" ] || ping -q -c 1 $first_router; then
  233.             if [ "$new_ip_address" != "$alias_ip_address" -a \
  234.                 -n "$alias_ip_address" ]; then
  235.                 ifconfig $interface:0 inet $alias_ip_address $alias_subnet_arg
  236.                 route add -host $alias_ip_address dev $interface:0
  237.             fi
  238.         
  239.             for router in $new_routers; do
  240.                 route add default dev $interface gw $router $metric_arg
  241.             done
  242.  
  243.             make_resolv_conf
  244.         else
  245.             # Changed from 'ifconfig $interface inet 0 down' - see Debian bug #144666
  246.             ifconfig $interface inet 0
  247.             exit_with_hooks 2 "$@"
  248.         fi
  249.  
  250.         ;;
  251. esac
  252.  
  253. exit_with_hooks 0
  254.